1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 import java.io.*;
53 import java.text.*;
54 import java.util.*;
55
56 public class ValidateISO4217 {
57
58 static final int ALPHA_NUM = 26;
59
60 static final byte UNDEFINED = 0;
61 static final byte DEFINED = 1;
62 static final byte SKIPPED = 2;
63
64
65 static final String datafile = "tablea1.txt";
66
67
68 static byte[] codes = new byte[ALPHA_NUM * ALPHA_NUM];
69
70 static final String[][] additionalCodes = {
71
72 {"AQ", "", "", "0"},
73
74
75
76
77
78
79 {"GS", "GBP", "826", "2"},
80
81
82 {"AX", "EUR", "978", "2"},
83 {"PS", "ILS", "376", "2"},
84
85
86 {"JE", "GBP", "826", "2"},
87 {"GG", "GBP", "826", "2"},
88 {"IM", "GBP", "826", "2"},
89 {"BL", "EUR", "978", "2"},
90 {"MF", "EUR", "978", "2"},
91 };
92
93
94 static final String otherCodes =
95 "ADP-AFA-ATS-AYM-BEF-BGL-BOV-BYB-CLF-CYP-DEM-ESP-FIM-FRF-GRD-GWP-IEP-ITL-LUF-MGF-MTL-MXV-NLG-PTE-RUR-SDD-SIT-SRG-TPE-TRL-VEF-USN-USS-XAG-XAU-XBA-XBB-XBC-XBD-XDR-XFO-XFU-XPD-XPT-XTS-XXX-YUM-ZWN";
96
97 static boolean err = false;
98
99 static Set<Currency> testCurrencies = new HashSet<Currency>();
100
101 public static void main(String[] args) throws Exception {
102 CheckDataVersion.check();
103 test1();
104 test2();
105 getAvailableCurrenciesTest();
106
107 if (err) {
108 throw new RuntimeException("Failed: Validation ISO 4217 data");
109 }
110 }
111
112 static void test1() throws Exception {
113
114 try (FileReader fr = new FileReader(new File(System.getProperty("test.src", "."), datafile));
115 BufferedReader in = new BufferedReader(fr))
116 {
117 String line;
118 SimpleDateFormat format = null;
119
120 while ((line = in.readLine()) != null) {
121 if (line.length() == 0 || line.charAt(0) == '#') {
122 continue;
123 }
124
125 StringTokenizer tokens = new StringTokenizer(line, "\t");
126 String country = tokens.nextToken();
127 if (country.length() != 2) {
128 continue;
129 }
130
131 String currency;
132 String numeric;
133 String minorUnit;
134 int tokensCount = tokens.countTokens();
135 if (tokensCount < 3) {
136 currency = "";
137 numeric = "0";
138 minorUnit = "0";
139 } else {
140 currency = tokens.nextToken();
141 numeric = tokens.nextToken();
142 minorUnit = tokens.nextToken();
143 testCurrencies.add(Currency.getInstance(currency));
144
145
146 if (tokensCount > 3) {
147 if (format == null) {
148 format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
149 format.setTimeZone(TimeZone.getTimeZone("GMT"));
150 format.setLenient(false);
151 }
152 if (format.parse(tokens.nextToken()).getTime() <
153 System.currentTimeMillis()) {
154 currency = tokens.nextToken();
155 numeric = tokens.nextToken();
156 minorUnit = tokens.nextToken();
157 testCurrencies.add(Currency.getInstance(currency));
158 }
159 }
160 }
161 int index = toIndex(country);
162 testCountryCurrency(country, currency, Integer.parseInt(numeric),
163 Integer.parseInt(minorUnit), index);
164 }
165 }
166
167 for (int i = 0; i < additionalCodes.length; i++) {
168 int index = toIndex(additionalCodes[i][0]);
169 if (additionalCodes[i][1].length() != 0) {
170 testCountryCurrency(additionalCodes[i][0], additionalCodes[i][1],
171 Integer.parseInt(additionalCodes[i][2]),
172 Integer.parseInt(additionalCodes[i][3]), index);
173 testCurrencies.add(Currency.getInstance(additionalCodes[i][1]));
174 } else {
175 codes[index] = SKIPPED;
176 }
177 }
178 }
179
180 static int toIndex(String s) {
181 return ((s.charAt(0) - 'A') * ALPHA_NUM + s.charAt(1) - 'A');
182 }
183
184 static void testCountryCurrency(String country, String currencyCode,
185 int numericCode, int digits, int index) {
186 if (currencyCode.length() == 0) {
187 return;
188 }
189 testCurrencyDefined(currencyCode, numericCode, digits);
190
191 Locale loc = new Locale("", country);
192 try {
193 Currency currency = Currency.getInstance(loc);
194 if (!currency.getCurrencyCode().equals(currencyCode)) {
195 System.err.println("Error: [" + country + ":" +
196 loc.getDisplayCountry() + "] expected: " + currencyCode +
197 ", got: " + currency.getCurrencyCode());
198 err = true;
199 }
200
201 if (codes[index] != UNDEFINED) {
202 System.out.println("Warning: [" + country + ":" +
203 loc.getDisplayCountry() +
204 "] multiple definitions. currency code=" + currencyCode);
205 }
206 codes[index] = DEFINED;
207 }
208 catch (Exception e) {
209 System.err.println("Error: " + e + ": Country=" + country);
210 err = true;
211 }
212 }
213
214 static void testCurrencyDefined(String currencyCode, int numericCode, int digits) {
215 try {
216 Currency currency = currency = Currency.getInstance(currencyCode);
217
218 if (currency.getNumericCode() != numericCode) {
219 System.err.println("Error: [" + currencyCode + "] expected: " +
220 numericCode + "; got: " + currency.getNumericCode());
221 err = true;
222 }
223
224 if (currency.getDefaultFractionDigits() != digits) {
225 System.err.println("Error: [" + currencyCode + "] expected: " +
226 digits + "; got: " + currency.getDefaultFractionDigits());
227 err = true;
228 }
229 }
230 catch (Exception e) {
231 System.err.println("Error: " + e + ": Currency code=" +
232 currencyCode);
233 err = true;
234 }
235 }
236
237 static void test2() {
238 for (int i = 0; i < ALPHA_NUM; i++) {
239 for (int j = 0; j < ALPHA_NUM; j++) {
240 char[] code = new char[2];
241 code[0] = (char)('A'+ i);
242 code[1] = (char)('A'+ j);
243 String country = new String(code);
244 boolean ex;
245
246 if (codes[toIndex(country)] == UNDEFINED) {
247 ex = false;
248 try {
249 Currency.getInstance(new Locale("", country));
250 }
251 catch (IllegalArgumentException e) {
252 ex = true;
253 }
254 if (!ex) {
255 System.err.println("Error: This should be an undefined code and throw IllegalArgumentException: " +
256 country);
257 err = true;
258 }
259 } else if (codes[toIndex(country)] == SKIPPED) {
260 Currency cur = null;
261 try {
262 cur = Currency.getInstance(new Locale("", country));
263 }
264 catch (Exception e) {
265 System.err.println("Error: " + e + ": Country=" +
266 country);
267 err = true;
268 }
269 if (cur != null) {
270 System.err.println("Error: Currency.getInstance() for an this locale should return null: " +
271 country);
272 err = true;
273 }
274 }
275 }
276 }
277 }
278
279
280
281
282 static void getAvailableCurrenciesTest() {
283 Set<Currency> jreCurrencies = Currency.getAvailableCurrencies();
284
285
286 StringTokenizer st = new StringTokenizer(otherCodes, "-");
287 while (st.hasMoreTokens()) {
288 testCurrencies.add(Currency.getInstance(st.nextToken()));
289 }
290
291 if (!testCurrencies.containsAll(jreCurrencies)) {
292 System.err.print("Error: getAvailableCurrencies() returned extra currencies than expected: ");
293 jreCurrencies.removeAll(testCurrencies);
294 for (Currency c : jreCurrencies) {
295 System.err.print(" "+c);
296 }
297 System.err.println();
298 err = true;
299 }
300 }
301 }